package org.uva.streambuilder;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;


public class StreamConnector implements Drawable, Comparable<StreamConnector> {

	StreamModule begin, end;
	static ArrayList<StreamConnector> connectorList = null;
	
	@SuppressWarnings("unchecked")
	StreamConnector(StreamModule begin, StreamModule end)
	{
		this.begin = begin;
		this.end = end;
		
		if (connectorList == null)
			connectorList = new ArrayList<StreamConnector>();
		
		connectorList.add(this);
		begin.outputConnectors.add(this);
		end.inputConnectors.add(this);
		
		Collections.sort(begin.outputConnectors);
		
		begin.numOutputs++;
		end.numInputs++;
		
		
	}
	
	
	public void drawConnector( Graphics g, int startx, int starty, int endx, int endy )
    {
    		int ymid = (endy-starty)/2 + starty;
    		g.setColor(Color.white);
    		g.drawLine(startx, starty, startx, ymid);
    		g.drawLine(startx, ymid, endx, ymid);
    		g.drawLine(endx, ymid, endx, endy);	
    	
    }
	
	public void draw(Graphics g) {
		int index1 = begin.outputConnectors.indexOf(this) + 1;
		int index2 = end.inputConnectors.indexOf(this) + 1;
		int startx = begin.getOutputPositionX(index1);
		int starty = begin.getOutputPositionY();
		int endx = end.getInputPositionX(index2);
		int endy = end.getInputPositionY();
		
		drawConnector(g, startx, starty, endx, endy);

	}

	public int compareTo(StreamConnector o) {
		// TODO Auto-generated method stub
		return this.end.x - o.end.x ;
	}

}
